#include "Thread.h"

using namespace std;

void *callFunction(void *arg);

// Initialise thread.
Thread::Thread(tr1::function<void (void)> f) : thread_(), f_(f) {
  pthread_create(&thread_, NULL, callFunction, &f_);
}

// Kill thread.
Thread::~Thread() {
  pthread_cancel(thread_);
}

void Thread::join() {
  pthread_join(thread_, NULL);
}

void Thread::detach() {
  pthread_detach(thread_);
}

void Thread::abort() {
  pthread_cancel(thread_);
}

// Call the tr1::function.
void *callFunction(void *arg) {
  ThreadFunction *f(static_cast<ThreadFunction *>(arg));
  (*f)();
  return NULL;
}

